package com.diapoc.androidteacher.ColorProgressBar;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.diapoc.androidteacher.R;

public class Activity_ColorProgressBar extends Activity {

	private Context context;
	private String TAG = "MainActivity  ";

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

		}

	};

	private void init() {
		context = this;
		initLayout();
	}

	int rate = 0;
	ColorProgressBar cpb;

	Handler hd = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

		}

	};

	boolean isInit = false;
	boolean isPause;
	boolean isFinish = false;
	Thread th = new Thread() {
		public void run() {
			try {
				while (rate < 100) {
					while (isPause) {
						sleep(20);
					}
					rate++;
					cpb.setState(ColorProgressBar.STATE_START);
					cpb.setText(rate + "%");
					cpb.setRate(rate);

					sleep(50);

				}
				cpb.setState(ColorProgressBar.STATE_FINISH);
				cpb.setText("下载完成");
				cpb.setRate(100);
				isFinish = true;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	};

	private void initLayout() {
		cpb = (ColorProgressBar) findViewById(R.id.cpb);

		// 初始化
		cpb.setState(ColorProgressBar.STATE_INIT);
		cpb.setText("下载");
		cpb.setRate(100);
		cpb.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!isInit) {
					isPause = false;
					isInit = true;
					th.start();
				} else {
					if (!isFinish) {
						isPause = !isPause;
						if (isPause) {
							cpb.setState(ColorProgressBar.STATE_PAUSE);
							cpb.setText("暂停");
						} else {
							cpb.setState(ColorProgressBar.STATE_START);
						}
					}
				}
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.v("test", TAG + "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_colorprogressbar);
		init();
	}

	public void onStart() {
		Log.v("test", TAG + "onStart");
		super.onStart();
	}

	public void onResume() {
		Log.v("test", TAG + "onResume");
		super.onResume();
	}

	public void onPause() {
		Log.v("test", TAG + "onPause");
		super.onPause();
	}

	public void onStop() {
		Log.v("test", TAG + "onStop");
		super.onStop();
	}

	public void onDestroy() {
		Log.v("test", TAG + "onDestroy");
		super.onDestroy();
	}

}
